package com.mfh.ser;

import java.io.*;

public class Main {

    private static Developer getDeveloper() {
        Developer developer = new Developer();
        developer.setName("Jon");
        developer.setAge(22);
        developer.setGender(1);
        developer.setCode("bug");
        Men men = new Men();
        men.setSuperman(true);
        developer.setMen(men);
        return developer;
    }

    public static void main(String[] args) {
        String fileName = "text1";
        //序列化对象
//        serial(fileName);
        //反序列化对象
//        deserialization(fileName);
        String className = new String(new byte[]{0x63, 0x6F, 0x6D, 0x2E, 0x6D, 0x66, 0x68, 0x2E, 0x73, 0x65, 0x72, 0x2E, 0x44, 0x65, 0x76, 0x65, 0x6C, 0x6F, 0x70, 0x65, 0x72});
        String codeName = new String(new byte[]{0x63, 0x6F, 0x64, 0x65});
        String innerClass = new String(new byte[]{0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3B});
        String menName = new String(new byte[]{0x6D , 0x65 , 0x6E});
        String MenPath = new String(new byte[]{0x4C , 0x63 , 0x6F , 0x6D , 0x2F , 0x6D , 0x66 , 0x68 , 0x2F , 0x73, 0x65 , 0x72 , 0x2F , 0x4D , 0x65 , 0x6E , 0x3B});
    }

    private static void serial(String fileName) {
        //构造程序员
        Developer developer = getDeveloper();
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(fileName);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(developer);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(fos, oos);
        }
    }

    private static void deserialization(String fileName) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(fileName);
            ois = new ObjectInputStream(fis);
            Developer developer = (Developer) ois.readObject();
            System.out.println(developer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(fis, ois);
        }
    }

    private static void close(OutputStream... oss) {
        if (oss == null) {
            return;
        }
        for (OutputStream os : oss) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    os = null;
                }
            }
        }
    }

    private static void close(InputStream... iss) {
        if (iss == null) {
            return;
        }
        for (InputStream is : iss) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    is = null;
                }
            }
        }
    }
}
